package com.ruoyi;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Author：chengyu
 * @Package：com.ruoyi
 * @Project：RuoYi-Vue-master
 * @name：BinarySearch
 * @Date：2024/10/27 9:19
 * @Filename：BinarySearch
 */
public class BinarySearch {
//    @Test
    public void testBinarySearch() {
        int[] array = {1, 2, 3, 4, 5, 7, 7, 8};
        int target = 7;
        int index = binarySearch(array, target);
        System.out.println(array[index]);
        System.out.println(array[6]);
    }
    /**
    *@Date：2024/10/27
    *@Author：chengyu
    *@fun：二分查找
    */
    int binarySearch(int[] nums, int target){
        int i = 0, j = nums.length - 1;
        while (i <= j) {
            int m = i + (j - i) / 2;
            if (nums[m] < target)
                i = m + 1;
            else if (nums[m] > target)
                j = m - 1;
            else
                return m;
        }
        return -1;
    }
}














